# @Time : 2024-09-29 20:50
# @Author : Fioman 
# @Phone : 13149920693
"""
设备状态监控,每个设备都创建一个线程,用来监控设备的状态,包括连接还有报警信息等,这里应该是一个单例类,就是全局只有这样一个
单例类就可以了.
"""
import threading

import gevent

import app_config
from app.device_state import DeviceStates
from common.produce_logger import ProduceLogger
from db_tools.db_handler import DbHandler

device_states = DeviceStates()
logger = ProduceLogger()
db = DbHandler()


class StateListener(object):
    """
    数据库操作类
    """

    def __init__(self, left_cam, mid_cam, right_cam, cutter_cam01, cutter_cam02, offline_zmc, cam_zmc,
                 cbj_zmc, cutter_zmc):
        print("StateListener.__init__() 进行初始化")
        self.__init_flag = True
        self.left_cam = left_cam
        self.mid_cam = mid_cam
        self.right_cam = right_cam
        self.cutter_cam01 = cutter_cam01
        self.cutter_cam02 = cutter_cam02
        self.offline_zmc = offline_zmc
        self.cam_zmc = cam_zmc
        self.cbj_zmc = cbj_zmc
        self.cutter_zmc = cutter_zmc
        self.loop_duration = 0.8
        # 设备初始状态
        self.cam_zmc_is_run = False
        self.cam_zmc_is_rs = False
        self.cbj_zmc_is_run = False
        self.cbj_zmc_is_rs = False
        self.cutter_zmc_is_run = False
        self.cutter_zmc_is_rs = False
        self.offline_zmc_is_run = False
        self.offline_zmc_is_rs = False
        self.manager_thread = threading.Thread(target=self.thread_manager, name="manager_th")
        self.offline_zmc_thread = threading.Thread(target=self.offline_zmc_listener, name="offline_th")
        self.cam_zmc_thread = threading.Thread(target=self.cam_zmc_listener, name="cam_th")
        self.cbj_zmc_thread = threading.Thread(target=self.cbj_zmc_listener, name="cbj_th")
        self.cutter_zmc_thread = threading.Thread(target=self.cutter_zmc_listener, name="cutter_th")
        self.cameras_thread = threading.Thread(target=self.cameras_listener, name="camera_th")

    def offline_zmc_listener(self):
        pass

    def cam_zmc_listener(self):
        pass

    def cbj_zmc_listener(self):
        pass

    def cutter_zmc_listener(self):
        pass

    def cameras_listener(self):
        pass

    def thread_manager(self):
        """
        线程管理类,管理各个线程,防止线程中断后可以自动重新创建
        :param self:
        :return:
        """
        while app_config.is_main_alive:
            if (not device_states.cam_zmc_states.run and not device_states.cbj_zmc_states.run
                    and not device_states.cutter_zmc_states.run):
                device_states.stop_work_flag = True
            # 实时更新设备状态
            # send_to_ui_handler.send_device_state_to_ui(device_states.pack_state_to_dict())
            try:
                if not self.offline_zmc_thread.is_alive():
                    self.offline_zmc_thread = threading.Thread(target=self.offline_zmc_listener, name="offline_th")
                    self.offline_zmc_thread.setDaemon(True)
                    self.offline_zmc_thread.start()

                if not self.cam_zmc_thread.is_alive():
                    self.cam_zmc_thread = threading.Thread(target=self.cam_zmc_listener, name="cam_th")
                    self.cam_zmc_thread.setDaemon(True)
                    self.cam_zmc_thread.start()

                if not self.cbj_zmc_thread.is_alive():
                    self.cbj_zmc_thread = threading.Thread(target=self.cbj_zmc_listener, name="cbj_th")
                    self.cbj_zmc_thread.setDaemon(True)
                    self.cbj_zmc_thread.start()

                if not self.cutter_zmc_thread.is_alive():
                    self.cutter_zmc_thread = threading.Thread(target=self.cutter_zmc_listener, name="cutter_th")
                    self.cutter_zmc_thread.setDaemon(True)
                    self.cutter_zmc_thread.start()

                if not self.cameras_thread.is_alive():
                    self.cameras_thread = threading.Thread(target=self.cameras_listener, name="camera_th")
                    self.cameras_thread.setDaemon(True)
                    self.cameras_thread.start()

                if not device_states.web_data_is_init:
                    if device_states.is_login:
                        gevent.sleep(0.3)
                        # 如果登录成功,就去更新数据
                        device_states.web_show_data_init()
                        device_states.is_login = False
                gevent.sleep(2)
            except Exception as e:
                gevent.sleep(2)
                logger.debug("in thread_manager() error: {}".format(str(e)))
